package cc.mrbird.febs.common.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

public class HttpUtils {
    public static final String CHARSET = "UTF-8";

    // 发送get请求 url?a=x&b=xx形式
    public static String get(String url, String param) {
        String result = "";
        BufferedReader in = null;  /*字符缓冲输入流*/
        try {
            String urlName = "";
            if (param.length() != 0) {
                urlName = url + "?" + param;
            } else {
                urlName = url;
            }
            URL resUrl = new URL(urlName);
            URLConnection urlConnec = resUrl.openConnection();
            urlConnec.setRequestProperty("accept", "*/*");
            urlConnec.setRequestProperty("connection", "Keep-Alive");
            urlConnec
                    .setRequestProperty(
                            "user-agent",
                            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36");
            urlConnec.connect();
            Map<String, List<String>> map = urlConnec.getHeaderFields();
            for (String key : map.keySet()) {
                System.out.println(key + "--->" + map.get(key));
            }
            // 定义 BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(
                    urlConnec.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送get请求失败" + e);
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    // 发送post请求
    public static String post(String url, Map<String, Object> map) {
        String result = "";
        PrintWriter out = null;
        BufferedReader in = null;
        try {
            URL resUrl = new URL(url);
            URLConnection urlConnect = resUrl.openConnection();
            urlConnect.setRequestProperty("accept", "*/*");
            urlConnect.setRequestProperty("connection", "Keep-Alive");
            urlConnect.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 发送POST请求必须设置如下两行
            urlConnect.setDoInput(true);
            urlConnect.setDoOutput(true);

            out = new PrintWriter(urlConnect.getOutputStream());
            StringBuffer sbParams = new StringBuffer();
            if (map != null && map.size() > 0) {
                for (Map.Entry<String, Object> e : map.entrySet()) {
                    sbParams.append(e.getKey());
                    sbParams.append("=");
                    sbParams.append(e.getValue());
                    sbParams.append("&");
                }
            }
            out.print(sbParams.substring(0, sbParams.length() - 1));// 发送post参数
            out.flush();
            // 定义 BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(
                    urlConnect.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("post请求发送失败" + e);
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println(result);
        return result;
    }



}
